################################################################################
# Imports
################################################################################

import Education
from EducationGUIHelper import *
import EducationGroup
import pprint #TODO: Remove me
import ycp
ycp.import_module("SCR")
from ycp import SCR, Path

################################################################################
# Functions
################################################################################

class AccessTimeRestrictions:

  def __init__(self):
    
    self.enabled = False
    self.allowed = {}
    self.start_time = {}
    self.end_time = {}

    for day in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']:

      self.allowed[day] = True
      self.start_time[day] = '00:00'
      self.end_time[day] = '00:00'

    self.modified = False

  def set_enabled(self, enabled, loading=False):
    
    if (self.enabled != enabled):
      self.enabled = enabled

      if not loading:
        self.modified = True

  def allow_access_on(self,day):

    return self.allowed[day]

  def start_time_on(self, day):

    return self.start_time[day]

  def end_time_on(self, day):

    return self.end_time[day]

  def set_allowed_on(self, day, allowed, loading=False):

    if (self.allowed[day] != allowed):
      self.allowed[day] = allowed

      if not loading:
        self.modified = True

  def set_start_time_on(self, day, start_time, loading=False):

    if (self.start_time[day] != start_time):
      self.start_time[day] = start_time

      if not loading:
        self.modified = True

  def set_end_time_on(self, day, end_time, loading=False):

    if (self.end_time[day] != end_time):
      self.end_time[day] = end_time

      if not loading:
        self.modified = True

  def get_start_hour(self, day):
    return self.get_hour(self.start_time[day])

  def get_start_minute(self, day):
    return self.get_minute(self.start_time[day])

  def get_end_hour(self, day):
    return self.get_hour(self.end_time[day])

  def get_end_minute(self, day):
    return self.get_minute(self.end_time[day])
  
  def get_hour(self, time):
    parts = time.split(":")
    return int(parts[0])

  def get_minute(self, time):
    parts = time.split(":")
    return int(parts[1])

  def set_access_times_on(self, day, start_time, end_time, loading=False):

    start_hour = self.get_hour(start_time)
    start_min = self.get_minute(start_time)
    end_hour = self.get_hour(end_time)
    end_min = self.get_minute(end_time)

    if (((start_hour == end_hour) and (end_min < start_min)) or (start_hour > end_hour)):
      self.set_start_time_on(day, end_time, loading)
      self.set_end_time_on(day, start_time, loading)
    else:
      self.set_start_time_on(day, end_time, loading)
      self.set_end_time_on(day, start_time, loading)
      
  def get_table_data(self):

    access_times = []

    # Day   Access Allowed   Allowed Time Range

    for day in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]:
      if self.allow_access_on(day) and (self.start_time_on(day) != self.end_time_on(day)):
        access_times.append(item(day, [day, "Yes", self.start_time_on(day) + " to " + self.end_time_on(day)]))
      elif self.allow_access_on(day):
        access_times.append(item(day, [day, "Yes", "All day"]))
      else:
        access_times.append(item(day, [day, "No", "Not allowed"]))

    return access_times

  def save_state(self):

    self.undo_state = {}

    self.undo_state['enabled'] = self.enabled
    self.undo_state['allowed'] = self.allowed.copy()
    self.undo_state['start_time'] = self.start_time.copy()
    self.undo_state['end_time'] = self.end_time.copy()
    self.undo_state['modified'] = self.modified

  def restore_state(self):

    if (self.undo_state == None):
      return

    self.enabled = self.undo_state['enabled']
    self.allowed = self.undo_state['allowed'].copy()
    self.start_time = self.undo_state['start_time'].copy()
    self.end_time = self.undo_state['end_time'].copy()
    self.modified = self.undo_state['modified']

    self.undo_state = None

  def write_to_disk(self, group_name):

    path = ".edugroup.value." + group_name
    
    SCR.Write(Path(path + ".ACCESS_RESTRICTIONS_ENABLED"), self.enabled and "yes" or "no")

    for day in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']:
      SCR.Write(Path(path + "." + day.upper() + "_ALLOW_ACCESS"), self.allow_access_on(day) and "yes" or "no")
      SCR.Write(Path(path + "." + day.upper() + "_START_TIME"), self.start_time_on(day))
      SCR.Write(Path(path + "." + day.upper() + "_END_TIME"), self.end_time_on(day))

    #TODO: Configure access time restrictions on disk here

    
def get_restrictions_for_group(group_name):

  return Education.get_config('groups')[group_name].access_times

def get_all_groups_table_data():

  restrictions = []

  for group_name in EducationGroup.get_education_groups():

    group = EducationGroup.load_group(group_name)
    group_name_cell = icon_cell("yast-education-restrictions.png", group_name)

    restrictions.append(item(group_name, [group_name_cell, group.access_times.enabled and "Restricted" or "Unrestricted"]))

  return restrictions